package br.com.hotstar.util;

import java.io.IOException;

import javax.inject.Inject;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;

import org.hibernate.Session;

/**
 * Servlet Filter implementation class Transaction
 */
@WebFilter("/*")
public class Transaction implements Filter {

	@Inject
    private Session session;
   
  

	
	public void destroy() {}


	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		 try{
	            
	            session.beginTransaction();
	            chain.doFilter(request, response);
	            session.getTransaction().commit();
	        } catch (Exception ex){
	            if(session != null && session.getTransaction().isActive()){
	                session.getTransaction().rollback();
	            }

	        }
		
	}


	public void init(FilterConfig fConfig) throws ServletException {}

}
